import { baseApiUrl, path } from "../../utils/config";
import { userInfo, sign, signPost } from '../../utils/api';
import Toast from '@vant/weapp/toast/toast';
const app = getApp();
Page({
  data: {
    baseImageUrl: baseApiUrl,
    integral: 0,
    list: [],
    week: 0,
    cover: "",
    week_text: "",
    sheet_show: false
  },
  onLoad() {
    const date = new Date();
    let day = date.getDay();
    let text = ["日", "一", "二", "三", "四", "五", "六"];
    text = text[day];
    this.setData({
      week_text: `星期${text}`
    })
  },
  onShow() {
    this.getUserInfo();
    this.getList();
  },
  async getList() {
    let { week, thumbnail } = await sign();
    this.setData({ list: week.week, cover: thumbnail });
    // let list = week.week;
    // const currentDate = new Date();
    // const futureDates = ["今日", "明日"];
    // for (let i = 2; i < list.length; i++) {
    //   let futureDate = new Date(currentDate.getTime() + i * 24 * 60 * 60 * 1000);
    //   let formattedDate = futureDate.toISOString().slice(0, 10).replace(/-/g, "-");
    //   formattedDate = formattedDate.slice(5);
    //   futureDates.push(formattedDate);
    // };
    // futureDates.forEach((item, index) => list[index].day_name = item);
    // this.setData({ list, cover: thumbnail });
  },
  async getUserInfo() {
    let res = await userInfo();
    let { integral } = res.user;
    this.setData({ integral });
  },
  async submit() {
    await signPost();
    Toast.success('签到成功');
    this.getUserInfo();
    this.getList();
  },
  router(e) {
    let { url } = e.currentTarget.dataset;
    wx.navigateTo({ url })
  },
  changeSheet() {
    this.setData({ sheet_show: !this.data.sheet_show });
  },
  downloadFile() {
    let { cover } = this.data;
    wx.downloadFile({
      url: cover,
      success: (res) => {
        let filePath = res.tempFilePath;
        wx.saveImageToPhotosAlbum({
          filePath,
          success: (res) => Toast.success('保存成功'),
          fail: (err) => Toast('保存失败')
        })
      }
    })
  },
  onSelect() {
    wx.getSetting({
      success: (res) => {
        if (res.authSetting['scope.writePhotosAlbum']) {
          this.downloadFile();
          return false;
        }
        if (!res.authSetting['scope.writePhotosAlbum']) {
          wx.authorize({
            scope: 'scope.writePhotosAlbum',
            success: () => this.downloadFile(),
            fail: (err) => {
              wx.showModal({
                title: '授权保存',
                content: '拒绝授权,将无法正常保存图片',
                confirmText: "授权",
                success: (res) => {
                  if (res.confirm) {
                    wx.openSetting({
                      success: (res) => Toast.success('授权成功')
                    })
                  }
                }
              })
            }
          })
        }
      }
    })
    // wx.authorizeForMiniProgram({
    //   scope: 'scope.writePhotosAlbum',
    //   success: () => {

    //   }
    // })
  },
  onShareAppMessage() {
    let poster = app.globalData.poster;
    return {
      path,
      imageUrl: poster
    }
  },
  onShareTimeline() { }
})